using Microsoft.EntityFrameworkCore;
using PandaWebAPI.Models.ef;

namespace PandaWebAPI.Services
{
    public class PandaService : BaseService<Panda>, IPandaService
    {
        public PandaService(PandaSystemContext context) : base(context)
        {
        }

        public async Task<List<Panda>> GetPandasByBaseIdAsync(Guid baseId)
        {
            return await _dbSet
                .Include(p => p.Base)
                .Where(p => p.BaseId == baseId)
                .OrderBy(p => p.Name)
                .ToListAsync();
        }

        public async Task<List<Panda>> GetPandasByGenderAsync(string gender)
        {
            return await _dbSet
                .Include(p => p.Base)
                .Where(p => p.Gender == gender)
                .OrderBy(p => p.Name)
                .ToListAsync();
        }

        public async Task<List<Panda>> GetPandasByBirthDateRangeAsync(DateOnly startDate, DateOnly endDate)
        {
            return await _dbSet
                .Include(p => p.Base)
                .Where(p => p.BirthDate >= startDate && p.BirthDate <= endDate)
                .OrderBy(p => p.BirthDate)
                .ToListAsync();
        }

        public async Task<Panda> UpdatePandaImageAsync(Guid pandaId, string imageUrl)
        {
            var panda = await _dbSet.FindAsync(pandaId);
            if (panda == null)
                throw new KeyNotFoundException($"Panda with ID {pandaId} not found.");

            panda.ImageUrl = imageUrl;
            await _context.SaveChangesAsync();

            return panda;
        }

        public override async Task<Panda> CreateAsync(Panda panda)
        {
            // 设置默认值
            panda.PandaId = Guid.NewGuid();

            // 验证必填字段
            if (string.IsNullOrWhiteSpace(panda.Name))
                throw new ArgumentException("Panda name is required.");

            await _dbSet.AddAsync(panda);
            await _context.SaveChangesAsync();

            return panda;
        }

        public override async Task<Panda> UpdateAsync(Panda panda)
        {
            var existingPanda = await _dbSet.FindAsync(panda.PandaId);
            if (existingPanda == null)
                throw new KeyNotFoundException($"Panda with ID {panda.PandaId} not found.");

            // 更新字段
            existingPanda.Name = panda.Name;
            existingPanda.BaseId = panda.BaseId;
            existingPanda.Gender = panda.Gender;
            existingPanda.BirthDate = panda.BirthDate;
            existingPanda.Description = panda.Description;
            existingPanda.ImageUrl = panda.ImageUrl;

            // 验证必填字段
            if (string.IsNullOrWhiteSpace(existingPanda.Name))
                throw new ArgumentException("Panda name is required.");

            await _context.SaveChangesAsync();

            return existingPanda;
        }

        public override async Task<bool> DeleteAsync(Guid id)
        {
            var panda = await _dbSet.FindAsync(id);
            if (panda == null)
                return false;

            _dbSet.Remove(panda);
            await _context.SaveChangesAsync();

            return true;
        }
    }
} 